<?php

namespace addons\Patrol\api\modules\v1\controllers;

use addons\Patrol\common\models\PatrolPlace;
use addons\Patrol\common\models\PatrolPlan;
use common\helpers\ArrayHelper;
use common\helpers\ResultHelper;
use Yii;
use yii\data\ActiveDataProvider;
use common\enums\StatusEnum;
use api\controllers\OnAuthController;
use yii\db\Expression;

/**

 * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass;
 * @author 古月 <21931118@qq.com>
 */
class DefaultController extends OnAuthController
{
    public $modelClass = PatrolPlan::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $authOptional = ['index'];

    /**
     * 首页
     *
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        if(Yii::$app->request->get('date')=='9999') $where='';
        else
            $where = new Expression("FIND_IN_SET(:supplier, check_date)", [":supplier" => Yii::$app->request->get('date')]);
        return $dataProvider = new ActiveDataProvider([
            'query' => $this->modelClass::find()
                ->andWhere($where)
                ->andWhere(['member_id' => Yii::$app->user->identity->member_id])
                ->orderBy('classes_id asc')
                ->with('member','place','patrolClasses')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
//        $models = $dataProvider->getModels();//获取AR实例数组
//        foreach ($models as &$item){
//            $item['selected'] = $item['check_date'];
//        }
//        return $models;
    }
    public function actionState()//巡更状态
    {
        return ArrayHelper::arrayToArrays(\addons\Patrol\common\enums\PlanEnum::getMap());
    }
    public function actionPlace()//地点
    {
        $model = PatrolPlace::find()->where(['label' => Yii::$app->request->get('label')])->one();
        return $model;
    }
    public function actionClasses()//班次
    {
        return ArrayHelper::arrayToArrays(\addons\Patrol\common\models\PatrolClasses::getDropDown());
    }
    public function actionPlaceState()//巡更点状态
    {
        return ArrayHelper::arrayToArrays(\addons\Patrol\common\enums\PlaceEnum::getMap());
    }
    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['delete','create','update'])) {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}